#include "stdafx.h"

#include "EntryReceiptonTbl.h"
#include "../RealObject/InputReceiption.h"
#include "../RealObject/OutputReceiption.h"
#include "../RealObject/TransferReceiption.h"

CEntryReceiptionTbl::CEntryReceiptionTbl()
{
	m_sName = TABLE_INPUT_BILL;
}
bool CEntryReceiptionTbl::addNewReceiption(const CReceiption* pReceiption)
{
	if (pReceiption)
	{
		CString sQueryAdd;
		int nType = pReceiption->getTypeReceiption();
		if(nType == BILL_INPUT)
		{
		
		sQueryAdd.Format(_T("( '%s','%s', '%s', '%s','%s','%s')")
			,pReceiption->getCode(),pReceiption->getCode(),((CInputReceiption*)pReceiption)->getEditAcountCode(),pReceiption->getDateInput(),pReceiption->getDateEdit(),pReceiption->getListProduct());
		return insertRecord(TBL_BILL_INPUT,sQueryAdd);
		}
		else if(nType == BILL_OUT)
		{
			
			sQueryAdd.Format(_T("( '%s','%s', '%s', '%s','%s')")
				,pReceiption->getCode(),pReceiption->getAcountImplement(),((COutputReceiption*)pReceiption)->getCodeCustomer(),pReceiption->getDateInput(),pReceiption->getListProduct());
			return insertRecord(TBL_BILL_OUTPUT,sQueryAdd);
		}
		else if(nType == BILL_TRANS)
		{

			sQueryAdd.Format(_T("( '%s','%s', '%s', '%s','%s','%s','%s')")
				,pReceiption->getCode(),pReceiption->getAcountImplement(),((CTransReceiption*)pReceiption)->getRevAcountCode(),((CTransReceiption*)pReceiption)->getStoreSource(),((CTransReceiption*)pReceiption)->getStoreTarget(),pReceiption->getDateInput(),pReceiption->getListProduct());
			return insertRecord(TBL_BILL_TRANS,sQueryAdd);
		}
		
	}
	return false;
}

 bool CEntryReceiptionTbl::addOutputBill(CString sCode,CString sCodeStaff, CString sCodeCustomer,CString sDate, CString sListProduct)
 {
	 CString sQueryAdd;
	 sQueryAdd.Format(_T("( '%s','%s', '%s', '%s','%s')")
		 ,sCode,sCodeStaff,sCodeCustomer,sDate,sListProduct);
	 return insertRecord(TBL_BILL_OUTPUT,sQueryAdd);
 }

 bool CEntryReceiptionTbl::addTransferBill(CString sCode,CString sCodeStaffSend, CString sCodeStaffCatch,CString sStoreSource,CString sStoreTarget,CString sDate, CString sListProduct)
 {
	 CString sQueryAdd;
	 sQueryAdd.Format(_T("( '%s','%s', '%s', '%s','%s','%s','%s')")
		 ,sCode,sCodeStaffSend,sCodeStaffCatch,sStoreSource,sStoreTarget,sDate,sListProduct);
	 return insertRecord(TBL_BILL_TRANS,sQueryAdd);
 }
CReceiption* CEntryReceiptionTbl::getReceiption(CString sCode,int nTypeBill)
{
	CReceiption* pReceiption = NULL;
	MYSQL_RES * pResult = NULL;
	CString sNewQuery;
	mysql_query(conn,"SET character_set_results=utf8");
	sNewQuery.Format(_T("SELECT * FROM %s.%s WHERE %s ='%s'"),g_sDatabase,TABLE_INPUT_BILL,FIELD_CODE_BILL,sCode);
	CT2A ascii(sNewQuery, CP_UTF8);		
	mysql_query(conn,ascii.m_psz);
	pResult = mysql_use_result(conn);
	if (pResult)
	{
		MYSQL_ROW row = mysql_fetch_row(pResult);
		if (row)
		{
			
			if(nTypeBill == BILL_INPUT)
			{
				pReceiption = new CInputReceiption;
				pReceiption->setCode(CString(row[0]));
				pReceiption->setAcountImplement(CString(row[1]));
				((CInputReceiption*)pReceiption)->setEditAcountCode(CString(row[2]));
				pReceiption->setDateInput(CString(row[3]));
				pReceiption->setDateEdit(CString(row[4]));
				pReceiption->setListProduct(CString(row[5]));
				
			}
			else if (nTypeBill == BILL_OUT)
			{
				pReceiption = new COutputReceiption;
				pReceiption = new COutputReceiption;
				pReceiption->setCode(CString(row[0]));
				pReceiption->setAcountImplement(CString(row[1]));
				((COutputReceiption*)pReceiption)->setCodeCustomer(CString(row[2]));
				pReceiption->setDateInput(CString(row[3]));				
				pReceiption->setListProduct(CString(row[4]));				
			}
			else if(nTypeBill == BILL_TRANS)
			{
				pReceiption = new CTransReceiption;
			}
		}
		mysql_free_result(pResult);
	}
	return pReceiption;
}

vector<CReceiption*> CEntryReceiptionTbl::getListReceiption(CString sDateInput,CString sDateEnd,int nTypeBill)
{
	vector<CReceiption*> vtReceiption;

	MYSQL_RES * pResult = NULL;
	CString sNewQuery;
	mysql_query(conn,"SET character_set_results=utf8");
	if(nTypeBill == BILL_INPUT)
		sNewQuery.Format(_T("SELECT * FROM %s.%s WHERE %s >='%s' AND %s <='%s'"),g_sDatabase,TABLE_INPUT_BILL,FIELD_CODE_TIME_INPUT,sDateInput,FIELD_CODE_TIME_INPUT,sDateEnd);
	else if(nTypeBill == BILL_OUT)
		sNewQuery.Format(_T("SELECT * FROM %s.%s WHERE %s >='%s' AND %s <='%s'"),g_sDatabase,TABLE_OUT_BILL,FIELD_DATE_SELL_PHONE,sDateInput,FIELD_DATE_SELL_PHONE,sDateEnd);
	else if (nTypeBill == BILL_TRANS)
		sNewQuery.Format(_T("SELECT * FROM %s.%s WHERE %s >='%s' AND %s <='%s'"),g_sDatabase,TABLE_TRANS_BILL,FIELD_CODE_TIME_INPUT,sDateInput,FIELD_CODE_TIME_INPUT,sDateEnd);
	CT2A ascii(sNewQuery, CP_UTF8);		
	mysql_query(conn,ascii.m_psz);
	pResult = mysql_use_result(conn);
	if (pResult)
	{
		MYSQL_ROW row = NULL;
		while((row = mysql_fetch_row(pResult)) )
		{
			CReceiption* pReceiption = NULL;
			if(nTypeBill == BILL_INPUT)
			{
				pReceiption = new CInputReceiption;
				pReceiption->setCode(CString(row[0]));
				pReceiption->setAcountImplement(CString(row[1]));
				((CInputReceiption*)pReceiption)->setEditAcountCode(CString(row[2]));
				pReceiption->setDateInput(CString(row[3]));
				pReceiption->setDateEdit(CString(row[4]));
				pReceiption->setListProduct(CString(row[5]));
				vtReceiption.push_back(pReceiption);
			}
			else if (nTypeBill == BILL_OUT)
			{
				pReceiption = new COutputReceiption;
				pReceiption->setCode(CString(row[0]));
				pReceiption->setAcountImplement(CString(row[1]));
				((COutputReceiption*)pReceiption)->setCodeCustomer(CString(row[2]));
				pReceiption->setDateInput(CString(row[3]));				
				pReceiption->setListProduct(CString(row[4]));
				vtReceiption.push_back(pReceiption);
			}
			else if(nTypeBill == BILL_TRANS)
			{
				pReceiption = new CTransReceiption;
			}
			
		}
		mysql_free_result(pResult);
	}
	return vtReceiption;
}

 int CEntryReceiptionTbl::getMaxCode(int nTypeBill)
 {
	 int nMax = 0;
	 MYSQL_RES * pResult = NULL;
	  CString sTable = _T("");
	 if (nTypeBill == BILL_INPUT)	 
		sTable = TABLE_INPUT_BILL;	
	 else if (nTypeBill == BILL_OUT)	
		sTable = TABLE_OUT_BILL;
	 else if (nTypeBill == BILL_TRANS)
		sTable = TABLE_TRANS_BILL;

	 CString sNewQuery;
	 mysql_query(conn,"SET character_set_results=utf8");
	 sNewQuery.Format(_T("SELECT %s ,MAX(%s)FROM %s.%s "),FIELD_CODE_BILL,FIELD_CODE_BILL,g_sDatabase,sTable);
	 CT2A ascii(sNewQuery, CP_UTF8);		
	 mysql_query(conn,ascii.m_psz);
	 pResult = mysql_use_result(conn);
	 if (pResult)
	 {
		 MYSQL_ROW row = mysql_fetch_row(pResult);
		 if (row)
		 {
			 CString sTemplate;
			 sTemplate = row[1];
			 sTemplate = sTemplate.Mid(2);
			 nMax = _tstoi(sTemplate);
		 }
		 mysql_free_result(pResult);
	 }
	 return nMax;
 }
